/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View.maestros;

import DAO.ParametroDAO;
import Model.Parametro;
import Presenter.EnvioPedidoPresenter;
import Presenter.VehiculoPresenter;
import Principals.Validadores;
import View.components.Index;
import java.io.File;
import java.util.List;
import javax.swing.JDesktopPane;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author walter
 */
public class Frm_RegistrarVehiculo extends javax.swing.JDesktopPane {

    /**
     * Creates new form Frm_BuscarVehiculo
     */
    Validadores val = new Validadores();

    public Frm_RegistrarVehiculo() {
        initComponents();
        EnvioPedidoPresenter.actualizarEnvioPedido();
        val.desactivarAccionPegar(txtPlaca);
        List<Parametro> lstTipoVehiculo = ParametroDAO.getTipoVehiculos();
        for (Parametro p : lstTipoVehiculo) {
            cbxTipoVehiculo.addItem(p);
        }
        btnCargarVehiculo.setEnabled(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar2 = new javax.swing.JToolBar();
        btnToolNuevo = new javax.swing.JButton();
        btnToolModificar = new javax.swing.JButton();
        btnToolAlmacenar = new javax.swing.JButton();
        btnToolEliminar = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JToolBar.Separator();
        btnToolLimpar = new javax.swing.JButton();
        btnToolBuscar = new javax.swing.JButton();
        jSeparator4 = new javax.swing.JToolBar.Separator();
        btnToolCancelar = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        pnlBuscarEmpleado = new javax.swing.JPanel();
        lblTipoVehiculo = new javax.swing.JLabel();
        lblPlaca = new javax.swing.JLabel();
        txtPlaca = new javax.swing.JTextField();
        cbxTipoVehiculo = new javax.swing.JComboBox();
        lblAnhoFabricacion = new javax.swing.JLabel();
        cbxAnhoFabricacion = new com.toedter.calendar.JYearChooser();
        jPanel1 = new javax.swing.JPanel();
        lblArchivoCarga = new javax.swing.JLabel();
        txtArchivoCarga = new javax.swing.JTextField();
        btnSeleccionarArchivo = new javax.swing.JButton();
        btnCargarVehiculo = new javax.swing.JButton();
        barrita = new javax.swing.JProgressBar();

        setPreferredSize(new java.awt.Dimension(665, 435));

        jToolBar2.setFloatable(false);
        jToolBar2.setBorderPainted(false);

        btnToolNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Nuevo.png"))); // NOI18N
        btnToolNuevo.setEnabled(false);
        btnToolNuevo.setFocusable(false);
        btnToolNuevo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolNuevo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolNuevoActionPerformed(evt);
            }
        });
        jToolBar2.add(btnToolNuevo);

        btnToolModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Editar.png"))); // NOI18N
        btnToolModificar.setEnabled(false);
        btnToolModificar.setFocusable(false);
        btnToolModificar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolModificar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolModificarActionPerformed(evt);
            }
        });
        jToolBar2.add(btnToolModificar);

        btnToolAlmacenar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Guardar.png"))); // NOI18N
        btnToolAlmacenar.setFocusable(false);
        btnToolAlmacenar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolAlmacenar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolAlmacenar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolAlmacenarActionPerformed(evt);
            }
        });
        jToolBar2.add(btnToolAlmacenar);

        btnToolEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Eliminar.png"))); // NOI18N
        btnToolEliminar.setEnabled(false);
        btnToolEliminar.setFocusable(false);
        btnToolEliminar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolEliminar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolEliminarActionPerformed(evt);
            }
        });
        jToolBar2.add(btnToolEliminar);
        jToolBar2.add(jSeparator3);

        btnToolLimpar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Limpiar.png"))); // NOI18N
        btnToolLimpar.setFocusable(false);
        btnToolLimpar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolLimpar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolLimparActionPerformed(evt);
            }
        });
        jToolBar2.add(btnToolLimpar);

        btnToolBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Buscar.png"))); // NOI18N
        btnToolBuscar.setFocusable(false);
        btnToolBuscar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolBuscar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolBuscarActionPerformed(evt);
            }
        });
        jToolBar2.add(btnToolBuscar);
        jToolBar2.add(jSeparator4);

        btnToolCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Cancelar.png"))); // NOI18N
        btnToolCancelar.setToolTipText("");
        btnToolCancelar.setFocusable(false);
        btnToolCancelar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolCancelar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolCancelarActionPerformed(evt);
            }
        });
        jToolBar2.add(btnToolCancelar);

        pnlBuscarEmpleado.setBorder(javax.swing.BorderFactory.createTitledBorder("Registrar nuevo vehículo"));

        lblTipoVehiculo.setText("Tipo de Vehículo*:");

        lblPlaca.setText("Placa*:");

        txtPlaca.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtPlacaKeyTyped(evt);
            }
        });

        lblAnhoFabricacion.setText("Año de fabricación*:");

        cbxAnhoFabricacion.setMaximum(2200);
        cbxAnhoFabricacion.setStartYear(1990);
        cbxAnhoFabricacion.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                cbxAnhoFabricacionKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout pnlBuscarEmpleadoLayout = new javax.swing.GroupLayout(pnlBuscarEmpleado);
        pnlBuscarEmpleado.setLayout(pnlBuscarEmpleadoLayout);
        pnlBuscarEmpleadoLayout.setHorizontalGroup(
            pnlBuscarEmpleadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBuscarEmpleadoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlBuscarEmpleadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblTipoVehiculo)
                    .addComponent(lblPlaca, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(pnlBuscarEmpleadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtPlaca, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)
                    .addComponent(cbxTipoVehiculo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(43, 43, 43)
                .addComponent(lblAnhoFabricacion)
                .addGap(29, 29, 29)
                .addComponent(cbxAnhoFabricacion, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        pnlBuscarEmpleadoLayout.setVerticalGroup(
            pnlBuscarEmpleadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBuscarEmpleadoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlBuscarEmpleadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(cbxAnhoFabricacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(pnlBuscarEmpleadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblTipoVehiculo)
                        .addComponent(cbxTipoVehiculo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblAnhoFabricacion)))
                .addGap(18, 18, 18)
                .addGroup(pnlBuscarEmpleadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPlaca)
                    .addComponent(txtPlaca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(226, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Carga individual", pnlBuscarEmpleado);

        lblArchivoCarga.setText("Archivo de carga:");

        txtArchivoCarga.setEnabled(false);

        btnSeleccionarArchivo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Examinar.png"))); // NOI18N
        btnSeleccionarArchivo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSeleccionarArchivoActionPerformed(evt);
            }
        });

        btnCargarVehiculo.setText("Cargar");
        btnCargarVehiculo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCargarVehiculoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(101, 101, 101)
                        .addComponent(lblArchivoCarga)
                        .addGap(18, 18, 18)
                        .addComponent(txtArchivoCarga, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(32, 32, 32)
                        .addComponent(btnSeleccionarArchivo))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(292, 292, 292)
                        .addComponent(btnCargarVehiculo)))
                .addContainerGap(158, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(barrita, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(233, 233, 233))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblArchivoCarga)
                    .addComponent(txtArchivoCarga, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSeleccionarArchivo))
                .addGap(18, 18, 18)
                .addComponent(btnCargarVehiculo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(barrita, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(199, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Carga masiva", jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTabbedPane1)
                    .addComponent(jToolBar2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(9, 9, 9)
                .addComponent(jTabbedPane1)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void cambiarVista(JDesktopPane panel) {
        ((Index) (this.getTopLevelAncestor())).changeDesktopPane(panel);
    }

    private void btnToolNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolNuevoActionPerformed
        cambiarVista(new Frm_RegistrarVehiculo()); // <---------------------------- ESTO SE DEBE CAMBIAR
    }//GEN-LAST:event_btnToolNuevoActionPerformed

    private void btnToolModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolModificarActionPerformed
    }//GEN-LAST:event_btnToolModificarActionPerformed

    private void btnToolBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolBuscarActionPerformed
        cambiarVista(new Frm_BuscarVehiculo());
    }//GEN-LAST:event_btnToolBuscarActionPerformed

    private void btnToolEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolEliminarActionPerformed
        cambiarVista(new Frm_EliminarVehiculo());
    }//GEN-LAST:event_btnToolEliminarActionPerformed

    private void btnToolCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolCancelarActionPerformed
        cambiarVista(new Frm_BuscarVehiculo());
    }//GEN-LAST:event_btnToolCancelarActionPerformed

    private void btnToolAlmacenarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolAlmacenarActionPerformed
        String placa = txtPlaca.getText();
        Parametro tipoVehiculo = (Parametro) cbxTipoVehiculo.getSelectedItem();
        int anho = cbxAnhoFabricacion.getYear();
        if (VehiculoPresenter.verificarPlaca(placa) <= 0) {
            if (!"".equals(placa)) {
                VehiculoPresenter.registrarVehiculo(placa, tipoVehiculo, anho);
                JOptionPane.showMessageDialog(this, "Se registró el vehículo de placa " + placa);
                cambiarVista(new Frm_BuscarVehiculo());
                btnToolLimparActionPerformed(null);
            } else {
                JOptionPane.showMessageDialog(this, "Debe completar todos los campos obligatorios");
            }
        } else {
            JOptionPane.showMessageDialog(this, "La placa ingresada ya se encuentra registrada en el sistema");
        }
    }//GEN-LAST:event_btnToolAlmacenarActionPerformed

    private void btnToolLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolLimparActionPerformed
        cbxTipoVehiculo.setSelectedIndex(-1);
        cbxAnhoFabricacion.setYear(2000);
        txtPlaca.setText("");
    }//GEN-LAST:event_btnToolLimparActionPerformed

    private void cbxAnhoFabricacionKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cbxAnhoFabricacionKeyTyped
        val.validaSoloNumeros(evt);
    }//GEN-LAST:event_cbxAnhoFabricacionKeyTyped

    private void txtPlacaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPlacaKeyTyped
        val.validaPlaca(evt);
        val.validaMaxSize(evt, 7, txtPlaca.getText());
    }//GEN-LAST:event_txtPlacaKeyTyped

    private void btnSeleccionarArchivoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSeleccionarArchivoActionPerformed
        JFileChooser jfcSeleccionarArchvivo = new JFileChooser();
        int status = jfcSeleccionarArchvivo.showOpenDialog(null);
        if (status == JFileChooser.APPROVE_OPTION) {
            File selectedFile = new File(jfcSeleccionarArchvivo.getSelectedFile().getAbsolutePath());
            txtArchivoCarga.setText(selectedFile.getAbsolutePath());
            btnCargarVehiculo.setEnabled(true);
        } else if (status == JFileChooser.CANCEL_OPTION) {
            System.out.println(JFileChooser.CANCEL_OPTION);
            btnCargarVehiculo.setEnabled(false);
        }
    }//GEN-LAST:event_btnSeleccionarArchivoActionPerformed

    private void btnCargarVehiculoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCargarVehiculoActionPerformed
        String path = txtArchivoCarga.getText() ;
        VehiculoPresenter.barrita = barrita ;
        VehiculoPresenter.registrarVehiculo( path ) ; 
        JOptionPane.showMessageDialog( this , "Se registraron los vehiculos con éxito" ) ;
        btnCargarVehiculo.setEnabled(false);
        txtArchivoCarga.setText("");
        cambiarVista(new Frm_BuscarVehiculo());
    }//GEN-LAST:event_btnCargarVehiculoActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JProgressBar barrita;
    private javax.swing.JButton btnCargarVehiculo;
    private javax.swing.JButton btnSeleccionarArchivo;
    private javax.swing.JButton btnToolAlmacenar;
    private javax.swing.JButton btnToolBuscar;
    private javax.swing.JButton btnToolCancelar;
    private javax.swing.JButton btnToolEliminar;
    private javax.swing.JButton btnToolLimpar;
    private javax.swing.JButton btnToolModificar;
    private javax.swing.JButton btnToolNuevo;
    private com.toedter.calendar.JYearChooser cbxAnhoFabricacion;
    private javax.swing.JComboBox cbxTipoVehiculo;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JToolBar.Separator jSeparator3;
    private javax.swing.JToolBar.Separator jSeparator4;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JToolBar jToolBar2;
    private javax.swing.JLabel lblAnhoFabricacion;
    private javax.swing.JLabel lblArchivoCarga;
    private javax.swing.JLabel lblPlaca;
    private javax.swing.JLabel lblTipoVehiculo;
    private javax.swing.JPanel pnlBuscarEmpleado;
    private javax.swing.JTextField txtArchivoCarga;
    private javax.swing.JTextField txtPlaca;
    // End of variables declaration//GEN-END:variables
}
